<?php

namespace App\Http\Controllers\OpenShop\Pay;

use App\Enums\Check\CheckEnum;
use App\Enums\Pay\RmsPayEnum;
use App\Http\Controllers\BaseController;
use App\Http\Middleware\OpenShop\MerchantAppAuth;
use App\Http\Resources\OpenShop\InvestmentPromotion\OrderDetailResource;
use App\Models\Cruise\CruiseCheckAgainstModel;
use App\Models\OpenShop\InvestmentPromotion\OpenShopMainProcessModel;
use App\Service\OpenShop\Wap\SignOrderService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class PayController extends BaseController
{
    /**
     * H5支付
     * @param Request $request
     * @param string $orderNo
     * @return JsonResponse
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function wapPay(Request $request): JsonResponse
    {
        $orderNo    = (string)$request->get('orderNo');
        $sysType    = (int)$request->get('sysType'); // 4招商加盟
        $payChannel = (int)$request->get('payChannel');
        $h5BackUrl  = (string)$request->get('h5BackUrl');
        $price      = (int)$request->get('price'); // 金额，单位分
        $bizId      = (string)$request->get('bizId'); // 业务单号
        $shopId     = (int)$request->header('shopId');

        $data = (new SignOrderService($orderNo))->preparePay($sysType, $payChannel, $h5BackUrl, $price, RmsPayEnum::PAY_ACTION_WAP, $bizId, $shopId);

        return $this->success($data);
    }

    /**
     * app支付
     * @param Request $request
     * @return JsonResponse
     */
    public function appPay(Request $request): JsonResponse
    {
        $orderNo    = (string)$request->get('orderNo');
        $sysType    = (int)$request->get('sysType'); // 4招商加盟
        $payChannel = (int)$request->get('payChannel');
        $price      = (int)$request->get('price'); // 金额，单位分
        $bizId      = (string)$request->get('bizId'); // 业务单号
        $shopId     = (int)$request->header('shopId');
        $currentUid = (int)MerchantAppAuth::$userId;

        if ($sysType == CheckEnum::SYS_TYPE_ANNUAL_FEE) {
            $withoutDel = true;
        } else {
            $withoutDel = false;
        }

        $signOrderModel = null;
        if ($sysType == CheckEnum::SYS_TYPE_AGAINST) {
            $signOrderModel = OpenShopMainProcessModel::query()->with('signOrderRel')->where('shopId', $shopId)->orderByDesc('id')->first()->signOrderRel ?? null;
            $bizId = CruiseCheckAgainstModel::query()->where('id', $bizId)->value('serialNum') ?? '';
        }

        $data = (new SignOrderService($orderNo, $signOrderModel, false, $withoutDel))->preparePay($sysType, $payChannel, '', $price, RmsPayEnum::PAY_ACTION_APP, $bizId, $shopId, $currentUid);

        return $this->success($data);
    }

    /**
     * 支付状态查询
     * @param Request $request
     * @return JsonResponse
     */
    public function payStatus(Request $request, string $secretOrderNo): JsonResponse
    {
        $payNo = (string)$request->get('payNo');

        // orderNo需要解密
        $orderNo = OrderDetailResource::decodeSignUrl($secretOrderNo);
        if ($request->has('debugOrderNo')) {
            $orderNo = $request->get('debugOrderNo');
        }

        if ($request->has('orderNo')) {
            $orderNo = $request->get('orderNo');
        }

        $data = (new SignOrderService($orderNo))->payStatus($payNo);

        return $this->success($data);
    }

    /**
     * 支付状态查询 - 不用加密
     * @param Request $request
     * @return JsonResponse
     */
    public function payStatusCommon(Request $request): JsonResponse
    {
        $payNo   = (string)$request->get('payNo');
        $orderNo = (string)$request->get('orderNo');

        $data = (new SignOrderService($orderNo))->payStatus($payNo);

        return $this->success($data);
    }
}
